package com.fyt.ZhongYaoApp.service.impl;

import com.fyt.ZhongYaoApp.mapper.DoctorMapper;
import com.fyt.ZhongYaoApp.pojo.Doctor;
import com.fyt.ZhongYaoApp.pojo.DoctorCat;
import com.fyt.ZhongYaoApp.pojo.vo.DoctorByCatVO;
import com.fyt.ZhongYaoApp.pojo.vo.DoctorQueery;
import com.fyt.ZhongYaoApp.pojo.PageBean;
import com.fyt.ZhongYaoApp.service.DoctorService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public void add(Doctor doctor) {
        doctorMapper.add(doctor);
    }

    @Override
    public PageBean<DoctorQueery> doctorList(Integer pageNum,Integer pageSize) {

        // 创建PageBean对象
        PageBean<DoctorQueery> pageBean = new PageBean();

        // 开启分页查询pageHelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用Mapper
        List<DoctorQueery> list = doctorMapper.doctorList();
        Page<DoctorQueery> page = (Page<DoctorQueery>) (list);

        // 把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;
    }

    @Override
    public PageBean<DoctorByCatVO> doctorListByCat(DoctorCat doctorCat, Integer pageNum, Integer pageSize) {
        // 创建PageBean对象
        PageBean<DoctorByCatVO> pageBean = new PageBean();

        // 开启分页查询pageHelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用Mapper
        List<DoctorByCatVO> list = doctorMapper.doctorListByCat(doctorCat);
        Page<DoctorByCatVO> page = (Page<DoctorByCatVO>) (list);

        // 把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;
    }

    @Override
    public PageBean<DoctorByCatVO> doctorListByName(int hospitalId, Integer pageNum, Integer pageSize) {
        // 创建PageBean对象
        PageBean<DoctorByCatVO> pageBean = new PageBean();

        // 开启分页查询pageHelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用Mapper
        List<DoctorByCatVO> list = doctorMapper.doctorListByName(hospitalId);
        Page<DoctorByCatVO> page = (Page<DoctorByCatVO>) (list);

        // 把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;
    }

    /**
     * 字符串数字递增方法
     * @param str
     * @return
     */
    public static String incrementString(String str) {
        int numberIndex = -1;
        for (int i = str.length() - 1; i >= 0; i--) {
            if (Character.isDigit(str.charAt(i))) {
                numberIndex = i;
            } else {
                break;
            }
        }
        if (numberIndex == -1) {
            return str + "1";
        }
        int number = Integer.parseInt(str.substring(numberIndex));
        number++;
        return str.substring(0, numberIndex) + number;
    }

    @Override
    public PageBean<DoctorQueery> doctorIntroduction(Doctor doctor, Integer pageNum, Integer pageSize) {
        // 创建PageBean对象
        PageBean<DoctorQueery> pageBean = new PageBean();

        // 开启分页查询pageHelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用Mapper
        List<DoctorQueery> list = doctorMapper.doctorIntroduction(doctor);

        // 将调用到的擅长病症提取出来
        String desease = list.get(0).getDrection();
        System.out.println("提取到的字符串： " + desease);

        // 将字符串以，剪切，得到新串
        String deseases[] = desease.split("，");
        System.out.println("切割到的字符串： " + deseases.length);
        for (String s : deseases) {
            System.out.println("切割到的字符串详情： " + s);
        }

        // 将剪切新串封装
        Map<String,String> deseaseMap = new HashMap<>();
        String str = "desease1"; // 字符串key值初始化
        for (String s : deseases) {
            deseaseMap.put(str,s);// 插入
            str = incrementString(str);// 让字符串key值递增
        }
        System.out.println(deseaseMap);
        list.get(0).setGoodDisease(deseaseMap);

        Page<DoctorQueery> page = (Page<DoctorQueery>) (list);

        // 把数据填充到PageBean对象中
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(page.getResult());

        return pageBean;
    }


}
